<template>
  <div class="product-stock">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>库存管理</span>
          <div class="header-buttons">
            <el-button type="primary" @click="handleStockIn">入库</el-button>
            <el-button type="warning" @click="handleStockOut">出库</el-button>
            <el-button type="info" @click="handleStockCheck">库存盘点</el-button>
          </div>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="商品名称">
          <el-input v-model="searchForm.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="searchForm.category" placeholder="请选择分类">
            <el-option label="宠物食品" value="food" />
            <el-option label="宠物玩具" value="toy" />
            <el-option label="医疗用品" value="medical" />
          </el-select>
        </el-form-item>
        <el-form-item label="库存状态">
          <el-select v-model="searchForm.stockStatus" placeholder="请选择状态">
            <el-option label="充足" value="sufficient" />
            <el-option label="不足" value="insufficient" />
            <el-option label="告警" value="warning" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 库存列表 -->
      <el-table :data="stockList" border style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="商品ID" width="80" />
        <el-table-column label="商品图片" width="100">
          <template #default="scope">
            <el-image
              style="width: 50px; height: 50px"
              :src="scope.row.image"
              :preview-src-list="[scope.row.image]"
            />
          </template>
        </el-table-column>
        <el-table-column prop="name" label="商品名称" />
        <el-table-column prop="category" label="分类" width="100" />
        <el-table-column prop="stock" label="当前库存" width="100" />
        <el-table-column prop="minStock" label="最低库存" width="100" />
        <el-table-column prop="stockStatus" label="库存状态" width="100">
          <template #default="scope">
            <el-tag :type="getStockStatusType(scope.row.stockStatus)">
              {{ scope.row.stockStatus }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="lastUpdate" label="最后更新" width="180" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleStockDetail(scope.row)">
              库存明细
            </el-button>
            <el-button type="success" size="small" @click="handleAdjust(scope.row)">
              调整
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 入库/出库对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="stockForm" :rules="rules" ref="stockFormRef" label-width="100px">
        <el-form-item label="商品" prop="productId">
          <el-select 
            v-model="stockForm.productId" 
            placeholder="请选择商品"
            filterable
          >
            <el-option
              v-for="item in productOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input-number v-model="stockForm.quantity" :min="1" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="stockForm.remark" rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitStockForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  name: '',
  category: '',
  stockStatus: ''
})

// 库存列表数据
const stockList = ref([
  {
    id: 1,
    name: '高级猫粮 - 三文鱼味',
    category: '宠物食品',
    stock: 100,
    minStock: 50,
    stockStatus: '充足',
    lastUpdate: '2024-03-18 10:00:00',
    image: 'https://placeholder.com/100'
  },
  {
    id: 2,
    name: '狗狗磨牙玩具',
    category: '宠物玩具',
    stock: 30,
    minStock: 50,
    stockStatus: '不足',
    lastUpdate: '2024-03-18 10:00:00',
    image: 'https://placeholder.com/100'
  },
  {
    id: 3,
    name: '宠物医用消毒液',
    category: '医疗用品',
    stock: 55,
    minStock: 50,
    stockStatus: '告警',
    lastUpdate: '2024-03-18 10:00:00',
    image: 'https://placeholder.com/100'
  }
])

// 商品选项
const productOptions = ref([
  { id: 1, name: '高级猫粮 - 三文鱼味' },
  { id: 2, name: '狗狗磨牙玩具' },
  { id: 3, name: '宠物医用消毒液' }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('商品入库')
const stockFormRef = ref(null)

const stockForm = reactive({
  productId: '',
  quantity: 1,
  remark: ''
})

const rules = {
  productId: [{ required: true, message: '请选择商品', trigger: 'change' }],
  quantity: [{ required: true, message: '请输入数量', trigger: 'blur' }]
}

// 方法定义
const handleSearch = () => {
  ElMessage.success('搜索功能待实现')
}

const resetSearch = () => {
  searchForm.name = ''
  searchForm.category = ''
  searchForm.stockStatus = ''
}

const getStockStatusType = (status) => {
  const types = {
    '充足': 'success',
    '不足': 'danger',
    '告警': 'warning'
  }
  return types[status] || 'info'
}

const handleStockIn = () => {
  dialogTitle.value = '商品入库'
  dialogVisible.value = true
}

const handleStockOut = () => {
  dialogTitle.value = '商品出库'
  dialogVisible.value = true
}

const handleStockCheck = () => {
  ElMessage.success('库存盘点功能待实现')
}

const handleStockDetail = (row) => {
  ElMessage.success(`查看商品 ${row.name} 的库存明细`)
}

const handleAdjust = (row) => {
  ElMessage.success(`调整商品 ${row.name} 的库存`)
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}

const submitStockForm = async () => {
  if (!stockFormRef.value) return
  
  await stockFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success(dialogTitle.value === '商品入库' ? '入库成功' : '出库成功')
      dialogVisible.value = false
    }
  })
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-buttons {
  display: flex;
  gap: 10px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 